<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 查询数据  -->
        <!-- 通过数据绑定 queryInfo中的query请求参数，然后点击按钮调用接口重新获取数据即可 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable @clear='getGoodstList()' v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search"
            @click="getGoodstList()"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品区域 -->
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 数据展示表格 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="700">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间"
        width="180">
        <template slot-scope="scope">
          <!-- 过滤器就是将管道符后面的数据替换掉前面的数据 -->
          {{scope.row.upd_time | dateFormat}}
        </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
             <el-button type="primary" icon="el-icon-edit" circle size="mini"></el-button>
             <el-button type="danger" icon="el-icon-delete" circle size="mini" @click="removeGoodsList(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- current-page当前所在页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 12, 15]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        // 注意页面不能为空或者0
        pagenum: 1,
        pagesize: 5
      },
      // 获取商品列表后保存商品列表数据和商品总数量
      goodsList: [],
      total: 0
    }
  },
  created () {
    this.getGoodstList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodstList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$Message.error('获取商品列表失败')
      // 保存商品列表数据和商品总数量到本地
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听每页展示多少条数据，参数是默认得到的一个变化的新值
    handleSizeChange (newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getGoodstList()
    },
    // 监听当前所在页，参数是默认得到的一个变化的新值
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodstList()
    },
    // 点击按钮删除商品
    async removeGoodsList (listId) {
      const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(erro => {
        // console.log(erro)
        return erro
      })
      if (result !== 'confirm') return this.$Message.error('已取消删除')

      const { data: res } = await this.$http.delete(`goods/${listId}`)
      console.log(res)
      if (res.meta.status !== 200) return this.$Message.error('删除失败')
      this.$Message.success('删除成功')
      this.getGoodstList()
    },
    // 点击按钮跳转到添加商品路由组件
    addGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
